package com.sanuo.pico.adapter;


import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.finddreams.languagelib.SoundTouchListener;
import com.google.gson.Gson;
import com.menu.app.db.bean.CusUsage;
import com.menu.app.fragment.AlertDlg;
import com.menu.app.util.Utils;
import com.sanuo.pico.App;
import com.sanuo.pico.bean.WorkParam;
import com.sanuo.pico.databinding.CommonContentBinding;
import com.sanuo.pico.databinding.RecordItemBinding;
import com.sanuo.pico.event.VbEvent;
import com.sanuo.pico.frag.ParamDlg;

import java.util.ArrayList;
import java.util.List;

public class RecordAdapter extends RecyclerView.Adapter {

    List<CusUsage> list;
    SoundTouchListener listener = new SoundTouchListener();
    final List<Long> delete = new ArrayList<>();
    ParamDlg dlg;

    public RecordAdapter(ParamDlg dlg, List<CusUsage> list) {
        this.list = list;
        this.dlg = dlg;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecordItemBinding binding = RecordItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new Holder0(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof Holder0) {
            ((Holder0) holder).bind(position);
        }
        holder.itemView.setOnTouchListener(listener);
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    private void bindCommon(CommonContentBinding binding, CusUsage data, int p, RecyclerView.ViewHolder holder, WorkParam param) {
        binding.freq.setText(param.freq + "");
        binding.energy.setText(param.energy + "");
        binding.wave.setText(VbEvent.getName(param.wave));
        binding.spot.setText(param.length + " mm");

        if (param.red == 0) {
            binding.red.setText(com.menu.app.R.string.light_off);
        } else {
            binding.red.setText(Utils.intToRoman(param.red));
        }
        binding.index.setText((p + 1) + "");
        binding.delete.setOnClickListener(v -> {
            new AlertDlg().setMessage(App.getApp().getString(com.menu.app.R.string.delete_confirm)).setOnDismissListener((isOk, value) -> {
                if (isOk) {
                    delete.add(data.id);
                    list.remove(data);
                    int i = holder.getBindingAdapterPosition();
                    notifyItemRemoved(i);
                    notifyItemRangeChanged(i, getItemCount() - i);
                }
            }).show(dlg.getChildFragmentManager(), "");
        });
        binding.reuse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dlg.toWork(list.get(holder.getBindingAdapterPosition()));
            }
        });
    }

    class Holder0 extends RecyclerView.ViewHolder {
        RecordItemBinding binding;

        Holder0(RecordItemBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        void bind(int p) {
            CusUsage data = list.get(p);
            Gson g = Utils.getGson();
            WorkParam param = g.fromJson(g.toJson(data.info), WorkParam.class);
            bindCommon(binding.common, data, p, this, param);
            binding.shot.setText(data.point + "");
        }
    }

    public final List getDelete() {
        return delete;
    }
}
